package com.iot08.api.service;

import com.iot08.common.constant.Const;
import com.iot08.common.entity.app.Course;
import com.iot08.common.entity.app.Knowledge;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Description:
 * @author: Rock
 * @time: 2021/5/31 14:54
 */
@Slf4j
@Service
public class CourseService extends BaseService<Course> {

    /**
     * @time: 2021/5/31 14:58
     * @author: Rock
     * @version 1.0
     */
    public ResponseMap list() {
        Query query = new Query(Criteria.where("deleted").is(0));
        List<Course> knowledgeList = this.find(query.with(Sort.by(Sort.Order.asc("index"))));
        List<Map<String, Object>> data = new ArrayList<>();
        for (Course course : knowledgeList) {
            Map<String, Object> temp = new HashMap<>();
            temp.put("id", course.getId());
            temp.put("title", course.getName());
            temp.put("webUrl", course.getWebUrl());
            temp.put("imgUrl", course.getImgUrl());
            temp.put("originalPrice", course.getOriginalPrice());
            temp.put("price", course.getPrice());
            data.add(temp);
        }
        ResponseMap res = ResponseMap.success();
        res.put("courses", data);
        return res;
    }


}
